package cn.udesk.common.kettle.environment;

import java.util.List;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import cn.udesk.common.toolkit.Constant;
import cn.udesk.core.model.KTrans;
import cn.udesk.web.service.TransService;
import lombok.extern.log4j.Log4j;

@Log4j
public class StartInit implements InitializingBean {

  @Autowired
  TransService transService;

  @Override
  public void afterPropertiesSet() throws Exception {
    // 初始化环境***
    cn.udesk.common.kettle.environment.KettleInit.init();
    org.pentaho.di.core.KettleEnvironment.init();
    log.info("Kettle初始化环境成功");

    List<KTrans> list = transService.getList();
    list.forEach(kTrans -> {
      if (kTrans.getTransStatus() != null
          && kTrans.getTransStatus().equals(Constant.TRANS_RUNNING)) {
        transService.start(kTrans.getTransId());
        log.info("恢复任务：" + kTrans.getTransId() + "~" + kTrans.getTransName() + "~"
            + kTrans.getTransDescription());
      }
    });
  }

}
